﻿#include <iostream>
#include <cmath>
#include <ctime>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include "Bin.h"
#include "color.h"
#include "Palette.h"

using namespace cv;
using namespace std;

#pragma comment(lib, "opencv_core249d.lib")
#pragma comment(lib, "opencv_highgui249d.lib")
#pragma comment(lib, "opencv_imgproc249d.lib")


int main()
{
	Mat u8img = imread("F:\\workspace\\palette-based_photo_recoloring\\testpic\\test2.png");
	Mat u8res = u8img.clone();
	long start_time = clock();
	uchar *pu8Img = u8img.data;
	float sigma = 80.0f;
	const int bin_size = 16;
	const int bin_num = bin_size*bin_size*bin_size;
	ColorBin bin_vec[bin_num];
	for(int i = 0 ; i < bin_size ; i++)
	{
        for(int j = 0 ; j < bin_size ; j++)
		{
			for(int k = 0 ; k < bin_size ; k++)
			{
				ColorLab bincenter = RGB2Lab((i+0.5f)*16,(j+0.5f)*16,(k+0.5f)*16);
				bin_vec[i*16*16+j*16+k].setCenter(bincenter);
			}
		}
	}
	pu8Img = u8img.data;
	//allocate points
	for(int i = 0 ; i < u8img.rows ; i++)
	{
		for(int j = 0 ; j < u8img.cols ; j++,pu8Img+=3)
		{
			int ind_i,ind_j,ind_k;
			ind_i = pu8Img[2]/16;
			ind_j = pu8Img[1]/16;
			ind_k = pu8Img[0]/16;
			bin_vec[ind_i*16*16+ind_j*16+ind_k].InsertPoint(i,j);
		}
	}

	int K=5;
	Palette palette(K,bin_vec,bin_size);
	palette.Show_oriPalette(100);
	palette.EditPalette(2,RGB2Lab(200,60,0));
	palette.Show_editPalette(100);
    TransferColor(u8img, u8res, palette);
	long end_time = clock();
	cout<<"time : "<<(end_time-start_time)*1000.0f/CLOCKS_PER_SEC<<" (ms)"<<endl;
    imshow("INPUT",u8img);
	imshow("OUTPUT",u8res);
	waitKey(0);
	return 0;
}
